<?php

namespace app\api\controller;

use app\admin\model\Dynamic;
use app\admin\model\Slide;
use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Config;
use think\Request;

/**
 * 首页接口
 */
class Document extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected static $model = null;


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\admin\model\Document();
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        $pageSize = input('page_size', 15);
        $list = self::$model
            ->field('id,name,image,intro,link,view,createtime')
            ->where('status', 'normal')
            ->order('id desc')
            ->paginate($pageSize)->each(function ($item) {
                $item['image'] = make_full_url($item['image']);
                $item['createtime'] = date('Y-m-d', $item['createtime']);
            });

        $this->success('文章列表', $list);
    }

    /**
     * 获取文章信息
     */
    public function read()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->param('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $row = self::$model->where('id', $id)->find();
        if (!$row) {
            $this->error('该文章不存在!');
        }

        $row->view += 1;
        $row->save();

        $this->success('文章详情为', $row);
    }
}
